/*
 * Project Name : NOBUNAGA
 * Created on 2007/02/02
 */
package jp.sengoku.oda {
	import mx.controls.ToggleButtonBar;
	import mx.core.IFlexDisplayObject;
	import jp.sengoku.oda.HideyoshiButton;
	import flash.ui.Keyboard;
	import flash.ui.KeyLocation;
	import mx.controls.ButtonBar;
	import mx.controls.buttonBarClasses.ButtonBarButton;
	import mx.controls.Button;
	import mx.core.mx_internal;
	import mx.styles.CSSStyleDeclaration;
	import mx.styles.StyleManager;
	import flash.events.MouseEvent;
	use namespace mx_internal;

	/**
	 * 拡張ボタンバークラス
	 * 設定したショートカットキー押下で、Clickイベントが発生する
	 * 
	 * @author	morimotohiroki@gmail.com
	 */
	public class MitsuhideButtonBar extends ButtonBar {

		/**
		 * コンストラクタござる
		 */
		public function MitsuhideButtonBar() {
			super();		
		}
		
		// トグル設定保持フラグ
		private var _toggle:Boolean = false;

		/**
		 * トグル設定
		 * 
		 * @param	flg		フラグ
		 * @return	void
		 */		
		public function set toggle(flg:Boolean):void {
			_toggle = flg;
			invalidateDisplayList();
		}

		/**
		 * トグル設定取得
		 * 
		 * @return	Boolean
		 */		
		public function get toggle():Boolean {
			return _toggle;
		}

		/**
		 * override clickHandler
		 * 
		 * @param	event	MouseEvent
		 * @return	void
		 */		
		override protected function clickHandler(event:MouseEvent):void {

			// トグル設定があるときに、イベント発火したボタンの選択状態を変更する
			for each (var c:Button in this.getChildren()) {
				if (_toggle && c is Button) {
					c.selected = c == (event.target as Button);
				}
			}

			super.clickHandler(event);
		}

		/**
		 * override updateDisplayList
		 * 
		 * @param	unscaledWidth
		 * @param	unscaledHeight
		 * @return	void
		 */		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {

			// トグルフラグを確認してトグルの設定をする
			for each (var c:Button in this.getChildren()) {
				if (c is Button) {
					c.toggle = _toggle;
				}
			}
			
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		}
		
		/**
		 * @private
		 * 
		 * @see mx.controls.ButtonBar
		 */
	    override protected function createNavItem(
	                                        label:String,
	                                        icon:Class = null):IFlexDisplayObject {
			var newButton:HideyoshiButton = new HideyoshiButton();
	        newButton.focusEnabled = false;
	
	        var buttonStyleName:String = getStyle(buttonStyleNameProp);
	        var firstButtonStyleName:String = getStyle(firstButtonStyleNameProp);
	        var lastButtonStyleName:String = getStyle(lastButtonStyleNameProp);
	
	        if (!buttonStyleName)
	            buttonStyleName = "ButtonBarButton";
	        if (!firstButtonStyleName)
	            firstButtonStyleName = buttonStyleName;
	        if (!lastButtonStyleName)
	            lastButtonStyleName = buttonStyleName;
	
	        var n:int = numChildren;
	        if (n == 0) {
	            newButton.styleName = buttonStyleName;
	        } else {
	            newButton.styleName = lastButtonStyleName;
	            var cssStyleDeclaration:CSSStyleDeclaration =
	                StyleManager.getStyleDeclaration("." + lastButtonStyleName);
	
	            if (cssStyleDeclaration &&
	                !cssStyleDeclaration.getStyle("focusRoundedCorners")) {
	                newButton.setStyle("focusRoundedCorners", "tr br");
	            }
	
	            // Refresh the skins for the last button that was in this position.
	            var first:Boolean = (n == 1);
	            var lastButton:Button = Button(getChildAt(first ? 0 : n - 1));
	
	            if (first) {
	                lastButton.styleName = firstButtonStyleName;
	                cssStyleDeclaration =
	                    StyleManager.getStyleDeclaration("." + firstButtonStyleName);
	
	                if (cssStyleDeclaration &&
	                    !cssStyleDeclaration.getStyle("focusRoundedCorners")) {
	                    lastButton.setStyle("focusRoundedCorners", "tl bl");
	                }
	            } else {
	                lastButton.styleName = buttonStyleName;
	                cssStyleDeclaration =
	                    StyleManager.getStyleDeclaration("." + buttonStyleName);
	
	                if (cssStyleDeclaration &&
	                    !cssStyleDeclaration.getStyle("focusRoundedCorners")) {
	                    lastButton.setStyle("focusRoundedCorners", "");
	                }
	            }
	
	            lastButton.changeSkins();
	            lastButton.invalidateDisplayList();
	        }
	
	        if (label != null && label.length > 0) {
		        newButton.shortcutKey = label.charCodeAt(0);
	        }
	        
	        newButton.label = label;
	        newButton.setStyle("icon", icon);
	
	        newButton.addEventListener(MouseEvent.CLICK, clickHandler);
	
	        addChild(newButton);
	
	        resetNavItems();
	
	        return newButton;
	    }
	}	    
}